﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InHD.model;
using InHD.utilities;
using System.Data;

namespace InHD.dataaccess
{
    class CompanyInfoRepository
    {
        #region Fields

        readonly List<CompanyInfo> _companyInfos;
        //For DB Utils
        private DBUltils _db;
        string[] _paramName;
        string[] _value;
        DBUltils.ParamType[] _paramType;

        #endregion // Fields

        #region Constructor

        /// <summary>
        /// Creates a new repository of CompanyInfos.
        /// </summary>
        public CompanyInfoRepository()
        {
            _companyInfos = new List<CompanyInfo>();
            this._db = new DBUltils();
            if (!_db.connectDB(Constants.Security.DefaultDBUser, Constants.Security.DefaultDBPassword))
                throw new ArgumentNullException(Constants.Messages.CannotConnectDB);
        }

        #endregion // Constructor

        #region Public Interface

        /// <summary>
        /// Raised when a CompanyInfo is placed into the repository.
        /// </summary>
        public event EventHandler<CompanyInfoAddedEventArgs> CompanyInfoAdded;

        /// <summary>
        /// Places the specified CompanyInfo into the repository.
        /// If the CompanyInfo is already in the repository, an
        /// exception is not thrown.
        /// </summary>
        public string AddCompanyInfo(CompanyInfo companyInfo)
        {
            try
            {
                if (companyInfo == null)
                    throw new ArgumentNullException("companyInfo");

                if (!_companyInfos.Contains(companyInfo))
                {
                    //// Add new company info into database
                    if (_db == null)
                        throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                    _paramName = new string[]{
                                        "@name",
                                        "@address",
                                        "@phone",
                                        "@email",
                                        "@director",
                                        "@taxCode"};
                    _paramType = new DBUltils.ParamType[]{
                                                    DBUltils.ParamType.NStr,
                                                    DBUltils.ParamType.LNStr,
                                                    DBUltils.ParamType.Str,
                                                    DBUltils.ParamType.Str,
                                                    DBUltils.ParamType.NStr,
                                                    DBUltils.ParamType.Str
                                                    };
                    _value = new string[]{
                                    companyInfo.Name,
                                    companyInfo.Address,
                                    companyInfo.Phone,
                                    companyInfo.Email,
                                    companyInfo.Director,
                                    companyInfo.Taxcode
                                    };

                    _db.queryDB(Constants.StoreProcedures.CompanyInfo.AddCompanyInfo, _paramName, _paramType, _value, DBUltils.QueryType.Insert);
                    _companyInfos.Add(companyInfo);

                    if (this.CompanyInfoAdded != null)
                        this.CompanyInfoAdded(this, new CompanyInfoAddedEventArgs(companyInfo));
                }
            }
            catch (Exception e)
            {
                Logger.RecordMessage(e, MessageType.Error);
                return e.Message;
            }
            //System.Windows.MessageBox.Show("Add company info successfully!!!");
            return string.Empty;
        }

        public string EditCompany(CompanyInfo company)
        {
            try
            {
                if (company == null)
                    throw new ArgumentNullException("company");

                //// edit existing product which have customerUNK  == customer.customerUNK

                this._companyInfos[0] = company;
                if (_db == null)
                    throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                _paramName = new string[]{
                                        "@name",
                                        "@address",
                                        "@phone",
                                        "@email",
                                        "@director",
                                        "@taxCode"};
                _paramType = new DBUltils.ParamType[]{
                                                    DBUltils.ParamType.NStr,
                                                    DBUltils.ParamType.LNStr,
                                                    DBUltils.ParamType.Str,
                                                    DBUltils.ParamType.Str,
                                                    DBUltils.ParamType.NStr,
                                                    DBUltils.ParamType.Str
                                                    };
                _value = new string[]{
                                    company.Name,
                                    company.Address,
                                    company.Phone,
                                    company.Email,
                                    company.Director,
                                    company.Taxcode
                                    };

                _db.queryDB(Constants.StoreProcedures.CompanyInfo.UpdateCompanyInfo, _paramName, _paramType, _value, DBUltils.QueryType.Update);

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            //System.Windows.MessageBox.Show("Edit Company Info successfully!!!");
            return string.Empty;
        }

        /// <summary>
        /// Returns a shallow-copied list of all CompanyInfos in the repository.
        /// </summary>
        public List<CompanyInfo> GetCompanyInfos()
        {
            return new List<CompanyInfo>(_companyInfos);
        }

        public string LoadCompanyInfo()
        {
            //// connect to database to get company info
            //// if company info has already exist then return true
            //CompanyInfo dummie=CompanyInfo.CreateCompanyInfo("1","inhd","vietname","admin","inhd@inhd.com","0900000000");
            //_companyInfos.Add(dummie);
            try
            {
                if (_db == null)
                    throw new ArgumentNullException(Constants.Messages.DBNotInitialized);
                _paramName = new string[] { };
                _paramType = new DBUltils.ParamType[] { };
                _value = new string[] { };
                DataSet ds = _db.queryDB("GetAllCompany", _paramName, _paramType, _value, DBUltils.QueryType.Select);

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    CompanyInfo ci = CompanyInfo.CreateCompanyInfo(
                                                                    Convert.ToString(dr["taxCode"]),
                                                                    Convert.ToString(dr["name"]),
                                                                    Convert.ToString(dr["address"]),
                                                                    Convert.ToString(dr["director"]),
                                                                    Convert.ToString(dr["email"]),
                                                                    Convert.ToString(dr["phone"])
                                                                   );
                    this._companyInfos.Add(ci);
                }
            }
            catch (Exception e)
            {
                Logger.RecordMessage(e, MessageType.Error);
                return e.Message;
            }
            return string.Empty;
        }

        public bool CompanyInfoExist { get { return (this._companyInfos.Count > 0); } }

        public CompanyInfo CompanyInformation { get { return this.CompanyInfoExist ? this._companyInfos[0] : null; } }

        #endregion // Public Interface

        #region Private Helpers

        #endregion // Private Helpers
    }
}
